package com.easyui.common;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.easyui.DB.ConnectDB;
import com.easyui.DB.OperateDB;
import com.easyui.bean.Attachment;
import com.easyui.bean.HeadNote;

public class SelAttach {
	
	/**
	 * 已知项目ID
	 * 返回项目附件名称链表
	 * @param pno
	 * @return
	 */
	public List<Map> getPAttachName(int pno) {
		List<Object> plist = null;
		List<Map> initList = null;
		
		String sql = "select * from attachment where bno = ? and btype = ?";
		
System.out.println(sql);
		plist = new ArrayList<Object>();
		plist.add(pno);
		plist.add("project");
		OperateDB.setParmList(plist);
		
		initList = OperateDB.excuteQuery(sql);
		return initList;
	}
	
	/**
	 * 获得帖的附件
	 * @param nno
	 * @return
	 */
	public List<Map> getNAttachName(int nno) {
		List<Object> plist = null;
		List<Map> initList = null;
		
		String sql = "select * from attachment where bno = ? and btype = ?";
		
System.out.println(sql);
		plist = new ArrayList<Object>();
		plist.add(nno);
		plist.add("note");
		OperateDB.setParmList(plist);
		
		initList = OperateDB.excuteQuery(sql);
		
		return initList;
	}
	
	public boolean newAttach(Connection conn, String upno, int pno, int bno, String uptype, String btype, List<Map> attachFile) {
		List<Object> plist = null;
		boolean success = false;
		
		if(attachFile != null && attachFile.size() > 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String uptime = sdf.format(new java.util.Date());
			Map<String, String> map = new HashMap<String, String>();
			
			for(int i=0; i<attachFile.size(); i++) {
				plist = new ArrayList<Object>();
				map = attachFile.get(i);
				String aname = (String)map.get("fileName");
				String savename = (String)map.get("saveName");
				String filesize = (String)map.get("filesize");
				String comment = (String)map.get("comment");
System.out.println("c:" + comment);
				String size = "";
				if(size != null && size.trim().equals("")) {
					int tmp = Integer.parseInt(filesize.trim());
					size = "" + tmp/1024 + "kb";
				}
				String sql = "insert into attachment values(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
				plist.add(aname);
				plist.add(savename);
				plist.add(comment);
				plist.add(size);
				plist.add(uptime);
				plist.add(uptype);
				plist.add(upno);
				plist.add(btype);
				plist.add(bno);
				plist.add(pno);//pno字段
				
				OperateDB.setParmList(plist);//初始化paramList
				
				if(!OperateDB.update(conn,sql)) {
					success = false;
					break; // 跳出循环
				} else {
					System.out.println("insert attachment " + aname + " success.");
					success = true;
				}
			}
		} else {
			success = false;
		}
		
		return success;
	}
	
	/**
	 * 上传成果展示的文件
	 * @param role
	 * @param userID
	 * @param pno
	 * @param attachFile
	 * @return
	 */
	public boolean newAchive(String role, String userID, int pno, List<Map> attachFile) {
		boolean success = true;
		
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit = false;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交
			
			boolean attach = false;//是否有附件
			if(attachFile != null && attachFile.size() > 0) {
				attach = true;
			} else {
				attach = false;
			}
			
			if(attach) {//有附件
				//向attachment表插入数据.
				System.out.println("有附件");
				if(new SelAttach().newAttach(conn, userID, pno, pno, role, "achive", attachFile)) {
					success = true;
				} else {
					success = false;
				}
			} else {
				success = true;
			}
			
		} catch (SQLException e) {
			success = false;
			System.out.println("上传文件失败，详情如下：\n");
			e.printStackTrace();
		} catch (NumberFormatException e) {
			success = false;
			System.out.println("数据格式化异常，详情如下：\n");
			e.printStackTrace();
		} finally {
			try {
				if (success) {
					conn.commit();//手动提交
				} else {
					conn.rollback();//回滚
				}
				conn.setAutoCommit(autoCommit);// 恢复现场
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			OperateDB.close(conn);
		}
		
		System.out.println("add:" + success);
		
		return success;
	}
	
	/**
	 * 获得对应项目的成果
	 * @param pno
	 * @param orderBy
	 * @param sortName
	 * @return
	 */
	public List<Map> getAllAchive(int pno, String orderBy, String sortName) {
		List<Map> initList = null;
		List<Object> plist = null;
		
System.out.println(orderBy);
System.out.println(sortName);
		
		String sql = "";
		sql = "select * from attachment where btype = 'achive' and bno = " + pno + " order by " + sortName + " " + orderBy;
		
System.out.println(sql);
		OperateDB.setParmList(plist);//初始化paramList
		initList = OperateDB.excuteQuery(sql);
		
		return initList;
	}
	
	public List<Attachment> getSpeAchive(List<Map> initList, int pno, int page, int rows, String orderBy, String sortName) {
		List<Attachment> alist = null;
		List<Map> finalList = null;
		List<Object> plist = null;
		
		if(initList != null && initList.size() > 0) {
			int totalRecords = 0;
			int totalPage = 0;
			int pageSize = rows;//rows 一页显示几行
			int pageNo = page;//page 第几页
			
			totalRecords = initList.size();
			totalPage = (totalRecords + pageSize -1)/pageSize;
			if(totalPage < 1) {
				totalPage = 1;
			}
			int startPos = (pageNo -1) * pageSize;
			
			String sql = "";
			sql = "select * from attachment where btype = 'achive' and bno = " + pno + " order by " + sortName + " " + orderBy + 
			" limit " + startPos + ", " + pageSize;
			
System.out.println(sql);
			OperateDB.setParmList(plist);//初始化paramList
			finalList = OperateDB.excuteQuery(sql);
			
			if(finalList != null && finalList.size() > 0) {
				alist = new ArrayList<Attachment>();
				for(int i=0; i<finalList.size(); i++) {
					Map map = finalList.get(i);
					Attachment a = new Attachment();
					a.init(map);
					alist.add(a);
				}
			}
		}
		
		return alist;
	}
	
	public Map<String, String> getAttach(int ano) {
		Map<String, String> attach = null;
		List<Map> finalList = null;
		List<Object> plist = null;
		
		String sql = "select * from attachment where ano = ?";
System.out.println(sql);

		plist = new ArrayList<Object>();
		plist.add(ano);
		OperateDB.setParmList(plist);//初始化paramList
		finalList = OperateDB.excuteQuery(sql);
			
		if(finalList != null && finalList.size() > 0) {
			attach = new HashMap<String, String>();
			Map map = finalList.get(0);
			attach.put("aname", (String)map.get("aname"));
			attach.put("savename", (String)map.get("savename"));
		}
		
		return attach;
	}
	
	/**
	 * 删除文件
	 * @param ano
	 * @return
	 */
	public boolean deleteAttach(int ano){
		boolean success = false;
		List<Object> plist = null;
		
		if(ano != -1) {
			String	sql = "delete from attachment where ano = ?";

			plist = new ArrayList<Object>();	
			plist.add(ano);
			OperateDB.setParmList(plist);//初始化paramList
			
			success = OperateDB.update(sql);
		} else {
			success = false;
		}
		
		return success;
	}
}
